package com.qbd.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qbd.model.Photo;
import com.qbd.model.Product;
import com.qbd.service.PhotoService;
import com.qbd.service.ProductService;
import com.qbd.utils.StringUtil;

@Controller
@RequestMapping("/search")
public class SearchController {

	@Autowired
	private ProductService productService;
	@Autowired
	private PhotoService photoService;

	@RequestMapping("/show")
	public String Show(HttpServletRequest request, String province, String city, String area, String product) {
		Map<String, Object> map = new HashMap<>();
		if (province.length() == 32) {
			map.put("province", province);
		} else {
			map.put("province", null);
		}
		if (city.length() == 32) {
			map.put("city", city);
		} else {
			map.put("city", null);
		}
		if (area.length() == 32) {
			map.put("area", area);
		} else {
			map.put("area", null);
		}
		map.put("product", StringUtil.formatLike(product));
		List<Product> list = productService.selectByAll(map);
		System.out.println(list.size());
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i) != null) {
					List<Photo> lPhotos = photoService.selectByPrid1(list.get(i).getId());
					list.get(i).setPhotos(lPhotos);
				}
			}
		}
		request.setAttribute("list", list);
		return "home/show";
	}

	@RequestMapping("/desc")
	public String Desc(HttpServletRequest request, String id) {
		Map<String, Object> map = new HashMap<>();
		map.put("aid", id);
		List<Product> list = productService.selectByAid(map);
		for (int i = 0; i < list.size(); i++) {
			List<Photo> lPhotos = photoService.selectByPrid1(list.get(i).getId());
			list.get(i).setPhotos(lPhotos);
		}
		request.setAttribute("list", list);
		return "home/show";
	}
}
